package edu.pku.sei.modeler.uml.commands.sequence;

import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;

public class UMLExecutionSpecificationCreateCommand extends
		NestedElementCreateCommand {
	public static final int EXECUTION_WIDTH = 10;

	public UMLExecutionSpecificationCreateCommand(MetaNode node,
			MetaNode hostNode, Rectangle constraint) {
		super(node, hostNode, constraint);
	}

	@Override
	public boolean canExecute() {
		return super.canExecute()
				&& fatherNode.getModelElement() instanceof Lifeline;
	}

	@Override
	protected void updateNodeBounds() {
		int hostX = 0;
		int hostWidth = fatherNode.getWidth();

		hostX += hostWidth / 2;

		// regular operations
		node.setLocationX(hostX - EXECUTION_WIDTH / 2);
		node.setLocationY(constraint.getLocation().y);
		node.setWidth(EXECUTION_WIDTH);
		// node.setHeight(constraint.height);

	}

	@Override
	protected void undoModelElement() {
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		lifeline.getIncludedExecutionOccurrences().remove(
				node.getModelElement());

	}

	@Override
	protected void updateModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		lifeline.getIncludedExecutionOccurrences().add(modelElement);

		// MetaModel model = lifeline.getModel();
		// try {
		// EventOccurrence startEO = ((ModelFactory)
		// ModelRegistry.getRegistry().
		// getFactory(model)).createEventOccurrence();
		// startEO.g
		//		
		// } catch (IllegalArgumentException e) {
		// e.printStackTrace();
		// } catch (FactoryNotFoundException e) {
		// e.printStackTrace();
		// }

	}

}
